//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "OperatorFactory.h"
using namespace Cephei::QL::Methods::Finitedifferences;
#include <gen/QL/Methods/Finitedifferences/TridiagonalOperator.h>
#include <gen/QL/Math/Array.h>
#include <gen/QL/Processes/GeneralizedBlackScholesProcess.h>
using namespace Cephei::QL::Math;
using namespace Cephei::QL::Processes;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Methods::Finitedifferences::COperatorFactory::COperatorFactory (boost::shared_ptr<QuantLib::OperatorFactory>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phOperatorFactory = NULL;
#endif
	_ppOperatorFactory = &childNative;
    
}
Cephei::QL::Methods::Finitedifferences::COperatorFactory::COperatorFactory (QuantLib::OperatorFactory& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phOperatorFactory = NULL;
#endif
	_ppOperatorFactory = new boost::shared_ptr<QuantLib::OperatorFactory> (&childNative);
    
    _OperatorFactoryOwner = owner;
    
}

Cephei::QL::Methods::Finitedifferences::COperatorFactory::COperatorFactory (COperatorFactory^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phOperatorFactory = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppOperatorFactory = new boost::shared_ptr<QuantLib::OperatorFactory> (copy->GetShared());
        
    }
}
Cephei::QL::Methods::Finitedifferences::COperatorFactory::COperatorFactory (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phOperatorFactory = NULL;
#endif
	if (!t->IsSubclassOf(COperatorFactory::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Methods::Finitedifferences::COperatorFactory::COperatorFactory (QuantLib::Handle<QuantLib::OperatorFactory>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phOperatorFactory = &childNative;
	_ppOperatorFactory = &static_cast<boost::shared_ptr<QuantLib::OperatorFactory>>(childNative.currentLink());
    
    _OperatorFactoryOwner = owner;
}
Cephei::QL::Methods::Finitedifferences::COperatorFactory::COperatorFactory (QuantLib::Handle<QuantLib::OperatorFactory> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phOperatorFactory = &childNative;
	_ppOperatorFactory = &static_cast<boost::shared_ptr<QuantLib::OperatorFactory>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Methods::Finitedifferences::COperatorFactory::COperatorFactory (QuantLib::OperatorFactory childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phOperatorFactory = NULL;
#endif
	_ppOperatorFactory = new boost::shared_ptr<QuantLib::OperatorFactory> (new QuantLib::OperatorFactory (childNative));
    
}
#endif

Cephei::QL::Methods::Finitedifferences::COperatorFactory::~COperatorFactory ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppOperatorFactory != NULL)
    {
	    delete _ppOperatorFactory;
        _ppOperatorFactory = NULL;
    }
}
Cephei::QL::Methods::Finitedifferences::COperatorFactory::!COperatorFactory ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppOperatorFactory != NULL)
    {
	    delete _ppOperatorFactory;
    }
}
QuantLib::OperatorFactory& Cephei::QL::Methods::Finitedifferences::COperatorFactory::GetReference ()
{
    if (_ppOperatorFactory == NULL) throw REFNEW NativeNullException ();
	return **_ppOperatorFactory;
}
boost::shared_ptr<QuantLib::OperatorFactory>& Cephei::QL::Methods::Finitedifferences::COperatorFactory::GetShared ()
{
    if (_ppOperatorFactory == NULL) throw REFNEW NativeNullException ();
	return *_ppOperatorFactory;
}
QuantLib::OperatorFactory* Cephei::QL::Methods::Finitedifferences::COperatorFactory::GetPointer ()
{
    if (_ppOperatorFactory == NULL) throw REFNEW NativeNullException ();
	return &**_ppOperatorFactory;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::OperatorFactory>& Cephei::QL::Methods::Finitedifferences::COperatorFactory::GetHandle ()
{
	if (_phOperatorFactory == NULL)
	{
		_phOperatorFactory = new Handle<QuantLib::OperatorFactory> (*_ppOperatorFactory);
	}
	return *_phOperatorFactory;
}
#endif
bool Cephei::QL::Methods::Finitedifferences::COperatorFactory::HasNative () 
{
	return (_ppOperatorFactory != NULL);
}

Cephei::QL::Methods::Finitedifferences::ITridiagonalOperator^ Cephei::QL::Methods::Finitedifferences::COperatorFactory::GetOperator (Cephei::QL::Processes::IGeneralizedBlackScholesProcess^ process, Cephei::QL::Math::IArray^ grid, Double residualTime, Boolean timeDependent)
{
    CGeneralizedBlackScholesProcess^ _Cprocess;
    CArray^ _Cgrid;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cprocess = safe_cast<CGeneralizedBlackScholesProcess^> (process);
        _Cprocess->Lock();
        boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess>& _process = static_cast<boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess>&> (_Cprocess->GetShared ()); 
        _Cgrid = safe_cast<CArray^> (grid);
        _Cgrid->Lock();
        QuantLib::Array& _grid = static_cast<QuantLib::Array&> (_Cgrid->GetReference ()); 
        QuantLib::Time _residualTime = (QuantLib::Time)ValueHelper::Convert (residualTime); //a
        bool _timeDependent = (bool)ValueHelper::Convert (timeDependent); //a
    	QuantLib::TridiagonalOperator _rv = (QuantLib::TridiagonalOperator)(*_ppOperatorFactory)->getOperator ( _process,  _grid,  _residualTime,  _timeDependent );   
        Cephei::QL::Methods::Finitedifferences::CTridiagonalOperator^ _nrv = REFNEW Cephei::QL::Methods::Finitedifferences::CTridiagonalOperator (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprocess != nullptr) _Cprocess->Unlock();
        if (_Cgrid != nullptr) _Cgrid->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

